package backend_package;

import java.util.Arrays;

/**
 * @author Syrym Satanov - edit
 * @author Kyler Nelson
 * @date 12-1-14
 * 
 *       This class holds various packet constructors used by the other classes
 *       to communicate via the observer observable design pattern
 * 
 */
public class Packet {

	/**
	 * @author Kyler
	 * */
	public String tag;
	/**
	 * @author Kyler
	 * */
	public Object[] oarr;
	/**
	 * @author Kyler
	 * */
	public String username;
	/**
	 * @author Syrym
	 * */
	public int whichTable;

	/**
	 * This constructor is the basic packet that holds just a tag
	 * 
	 * @param tag
	 *            the tag that tells the observer where the packet is coming
	 *            from
	 * @author Kyler
	 */
	public Packet(String tag) {
		this(tag, "");
	}

	/**
	 * a constructor that takes in several arguments
	 * 
	 * @author Kyler
	 * @param tag
	 *            the tag that tells the observer where the packet is coming
	 *            from
	 * @param name
	 *            the name of the user currently logged in
	 */
	public Packet(String tag, String name) {
		this(tag, new Object[] {}, name);
	}

	/**
	 * a constructor that takes in several arguments
	 * 
	 * @author Kyler
	 * @param tag
	 *            the tag that tells the observer where the packet is coming
	 *            from
	 * @param oarr
	 *            an array of any type of object needing to be sent
	 */
	public Packet(String tag, Object[] oarr) {
		this(tag, oarr, "");
	}

	/**
	 * a constructor that takes in several arguments
	 * 
	 * @author Kyler
	 * @param tag
	 *            the tag that tells the observer where the packet is coming
	 *            from
	 * @param oarr
	 *            an array of any type of object needing to be sent
	 * @param name
	 *            the name of the user currently logged in
	 */
	public Packet(String tag, Object[] oarr, String name) {
		this(tag, oarr, name, 0);
	}

	/**
	 * a constructor that takes in several arguments
	 * 
	 * @author Kyler
	 * @author Syrym
	 * @param tag
	 *            the tag that tells the observer where the packet is coming
	 *            from
	 * @param oarr
	 *            an array of any type of object needing to be sent
	 * @param name
	 *            the name of the user currently logged in
	 * @param whichTable
	 *            the table of the analyst panel being accessed
	 */
	public Packet(String tag, Object[] oarr, String name, int whichTable) {
		this.tag = tag;
		this.oarr = oarr;
		this.username = name;
		this.whichTable = whichTable;
	}

	/**
	 * @author Kyler
	 * @return
	 */
	public String getTag() {
		return tag;
	}

	/**
	 * @author Kyler
	 * @return
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @author Syrym
	 * @return
	 */
	public int getWhichTable() {
		return whichTable;
	}


	/**
	 * Get the object act the given index	
	 * @author Kyler
	 * @param index
	 * @return oarr[index]
	 */
	public Object get(int index) {
		return oarr[index];
	}

	/**
	 * Returns whether the two Package objects are equal.
	 * 
	 * @author Kyler
	 * @param index
	 * @return
	 */
	public boolean equals(Object o) {
		if (!(o instanceof Packet)) {
			return false;
		}

		Packet p = (Packet) o;
		return this.getTag().equals(p.getTag())
				&& this.getUsername().equals(p.getUsername())
				&& Arrays.equals(this.oarr, p.oarr)
				&& this.getWhichTable() == p.getWhichTable();
	}
}
